package com.kanche.bigbaby.collection.dialog;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;

import com.umeng.analytics.MobclickAgent;

import java.util.Calendar;

/**
 * Created by nealmi on 6/11/14.
 */
public class DatePickerFragment extends KancheDialogFragment
        implements DatePickerDialog.OnDateSetListener {

    private static final String TAG = "Merchant:DatePickerFragment";
    private long maxDate = 0L;
    private int day;
    private int month;
    private int year;
    private OnDateSelectedListener onDateSelectedListener;
    private long minDate = 0L;

    public long getMaxDate() {
        return maxDate;
    }

    public void setMaxDate(long maxDate) {
        this.maxDate = maxDate;
    }

    public long getMinDate() {
        return minDate;
    }

    public void setMinDate(long minDate) {
        this.minDate = minDate;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void updateDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public OnDateSelectedListener getOnDateSelectedListener() {
        return onDateSelectedListener;
    }

    public void setOnDateSelectedListener(OnDateSelectedListener onDateSelectedListener) {
        this.onDateSelectedListener = onDateSelectedListener;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        if (year == 0)
            year = c.get(Calendar.YEAR);
        if (month == 0)
            month = c.get(Calendar.MONTH);
        if (day == 0)
            day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day){
            @Override
            protected void onStop() {

            }
        };
        datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "清空", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (onDateSelectedListener != null) {
                    onDateSelectedListener.onDateClean();
                }
            }
        });
        DatePicker datePicker = datePickerDialog.getDatePicker();
        if (maxDate != 0L)
            datePicker.setMaxDate(maxDate);
        if (minDate != 0L) {
//            long current = DateTime.now().withYear(year).withMonthOfYear(month+1).withDayOfMonth(day).getMillis();
//            if (minDate < current)
            datePicker.setMinDate(minDate);
//            else
//                datePicker.setMinDate(current);
        }
        try {
            java.lang.reflect.Field[] datePickerFields = datePicker.getClass().getDeclaredFields();
            for (java.lang.reflect.Field datePickerField : datePickerFields) {
                if ("mDaySpinner".equals(datePickerField.getName())
                        || "mDaySpinnerInput".equals(datePickerField.getName())) {
                    datePickerField.setAccessible(true);
                    Object dayPicker = new Object();
                    dayPicker = datePickerField.get(datePicker);
                    ((View) dayPicker).setVisibility(View.GONE);
                }
            }
        } catch (Exception e) {
            MobclickAgent.reportError(getActivity(), e);
        }
        return datePickerDialog;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (getDialog() == null) return;
        DatePicker datePicker = ((DatePickerDialog) getDialog()).getDatePicker();
        if (datePicker != null)
            datePicker.updateDate(year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        if (onDateSelectedListener != null) {
            onDateSelectedListener.onDateSelected(view, year, month, day);
        }
        // Do something with the date chosen by the user
    }


    public interface OnDateSelectedListener {
        void onDateSelected(DatePicker view, int year, int month, int day);

        void onDateClean();
    }


}